package com.hdw.task.controller;

import com.hdw.common.base.BaseException;
import com.hdw.common.util.SnowflakeIdWorker;
import com.hdw.common.vo.LayMsg;
import com.hdw.common.vo.PaginationVO;
import com.hdw.job.api.EtlRunService;
import com.hdw.job.api.EtlTaskOdsService;
import com.hdw.job.bean.model.EtlHandlerExec;
import com.hdw.job.bean.model.EtlTaskDw;
import com.hdw.job.bean.model.EtlTaskOds;
import com.hdw.job.bean.vo.EtlHandExecVO;
import com.hdw.job.constant.JobConstant;
import com.hdw.job.mapper.EtlHandlerExecMapper;
import com.hdw.task.api.JobExecService;
import com.hdw.task.core.model.XxlJobInfo;
import com.hdw.task.service.XxlJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/task/jobExec/")
@Api(tags = "任务运行")
public class JobExecController {

    @Autowired
    JobExecService jobExecService;
    @Autowired
    EtlRunService etlRunService;
    @Autowired
    EtlHandlerExecMapper etlHandlerExecMapper;
    @Autowired
    EtlTaskOdsService etlTaskOdsService;
    @Resource
    private XxlJobService xxlJobService;

    @ApiOperation(value = "手工执行任务")
    @RequestMapping(value = "jobExecByHand", method = RequestMethod.GET)
    public LayMsg jobExecByHand(String startDate, String endDate, String idTasks, String type) throws BaseException {
        startDate = startDate.replaceAll("-","");
        endDate = endDate.replaceAll("-","");
        Long groupId = SnowflakeIdWorker.getId();
        EtlHandlerExec exec = new EtlHandlerExec();
        exec.setDtBegin(new Date());
        exec.setDes(type+"["+startDate+"~"+endDate+"]");
        exec.setIdGroup(groupId);
        exec.setStatus("STARTING");
        exec.setEuTp(type);
        etlHandlerExecMapper.insertReturnId(exec);
        String[] ids = idTasks.split(",");
        boolean isSuccess = false;
        for(String id:ids) {
            if(id.equals("dayBasedataXxljob")) {
                isSuccess = jobExecService.dayBasedataExec(startDate,endDate,groupId);
            }
        }
        exec.setDtEnd(new Date());
        exec.setStatus(etlRunService.getStatus(groupId));
        exec.setTimes((exec.getDtEnd().getTime()-exec.getDtBegin().getTime())/1000);
        etlHandlerExecMapper.updateByPrimaryKeySelective(exec);
        return LayMsg.success().addMsg(isSuccess==true?"执行成功":"执行失败");
    }

    @ApiOperation(value = "获取可执行")
    @RequestMapping(value = "findTaskList", method = RequestMethod.GET)
    public LayMsg findUseByPage(Integer page, Integer limit, String jobType) {
        if (StringUtils.isEmpty(jobType)) {
            return LayMsg.fail().addMsg("jobType不正确");
        } else if (jobType.equals(JobConstant.JOB_TYPE_ODS) || jobType.equals(JobConstant.JOB_TYPE_DIM)) {
            EtlTaskOds etlTaskOds = new EtlTaskOds();
            etlTaskOds.setFgStop("0");
            etlTaskOds.setEuType(jobType.toLowerCase());
            PaginationVO pageVO = etlTaskOdsService.findPageByModel(etlTaskOds, page, limit);
            List<EtlHandExecVO> result = new ArrayList<>();
            List<EtlTaskOds> entryList = pageVO.getEntryList();
            for (EtlTaskOds taskOds : entryList) {
                EtlHandExecVO etlHandExecVO = new EtlHandExecVO();
                etlHandExecVO.setIdTask(taskOds.getIdTaskOds() + "");
                etlHandExecVO.setNaTask(taskOds.getNaTaskOds());
                etlHandExecVO.setDes(taskOds.getDesEvent());
                etlHandExecVO.setNaDbSou(taskOds.getNaDbSou());
                etlHandExecVO.setTbTar(taskOds.getTbTar());
                result.add(etlHandExecVO);
            }
            pageVO.setEntryList(result);
            return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
        } else if (jobType.equals(JobConstant.JOB_TYPE_XXLJOB)) {
            List<XxlJobInfo> list = xxlJobService.list();
            List<EtlHandExecVO> etlHandExecVOList = new ArrayList<>();
            EtlHandExecVO execVO;
            for (XxlJobInfo tb : list) {
                execVO = new EtlHandExecVO();
                execVO.setIdTask(tb.getExecutorHandler());
                execVO.setTbTar(tb.getExecutorHandler());
                execVO.setDes(tb.getJobDesc());
                execVO.setNaTask(tb.getExecutorHandler());
                execVO.setNaDbSou("本地库");
                etlHandExecVOList.add(execVO);
            }
            return LayMsg.success().addCount(etlHandExecVOList.size()).addData(etlHandExecVOList);
        }  else {
            return LayMsg.fail().addMsg("jobType不正确");
        }
    }
}
